<template>
    <div>
        <div class="form-item form-item-style1">
            <div class="label-title">
                <label>处置卡名称</label>
            </div>
            <div>
                <input type="text"
                       v-model="EmergencyDisposalName"
                       @change="onPullingDown"
                       placeholder="请输入处置卡名称">
            </div>
        </div>
        <h3>数量：{{listAllCount}}</h3>
        <noData v-if="listAllCount == 0"></noData>
        <div class="betterscrollWrapper">
            <vue-better-scroll class="betterScroll"
                               ref="scroll"
                               :scrollbar="scrollbarObj"
                               :pullDownRefresh="pullDownRefreshObj"
                               :pullUpLoad="pullUpLoadObj"
                               :startY="parseInt(startY)"
                               @pullingDown="onPullingDown"
                               @pullingUp="onPullingUp">
                <ul class="recursive-ul">
                    <li v-for="(item,index) in lists"
                        :key="index">
                        <div class="item"
                             v-if="item.children"
                             @click="openChild(item)">
                            <p>{{item.EmergencyDisposalName}}</p>
                            <p>
                                <x-icon type="ios-arrow-right"
                                        size="20"
                                        fill="#999"
                                        v-show="!item.open"></x-icon>
                                <x-icon type="ios-arrow-down"
                                        size="20"
                                        fill="#999"
                                        v-show="item.open"></x-icon>
                            </p>
                        </div>
                        <router-link :to="{path:'/emergencyCardDetail',query:{id:item.ID}}"
                                     class="item"
                                     v-else>
                            <p>{{item.EmergencyDisposalName}}</p>
                            <p>
                                <x-icon type="ios-arrow-right"
                                        size="20"
                                        fill="#999"></x-icon>
                            </p>
                        </router-link>
                        <ul class="recursive-ul"
                            v-for="(childItem,childIndex) in item.children"
                            :key="childIndex"
                            v-show="item.open">
                            <li>
                                <router-link :to="{path:'/emergencyCardDetail',query:{id:childItem.ID}}"
                                             class="item">
                                    <p>{{childItem.PostName}}</p>
                                    <p>
                                        <x-icon type="ios-arrow-right"
                                                size="20"
                                                fill="#999"></x-icon>
                                    </p>
                                </router-link>
                            </li>
                        </ul>
                    </li>
                </ul>
            </vue-better-scroll>
        </div>
        <myMenu></myMenu>
    </div>
</template>
<script>
import VueBetterScroll from 'vue2-better-scroll'
import myMenu from '@/commonComponents/menu'
export default {
    components: {
        VueBetterScroll,
        myMenu
    },
    data () {
        return {
            userInfo: {},
            // dropDown: false
            // 这个配置可以开启滚动条，默认为 false。当设置为 true 或者是一个 Object 的时候，都会开启滚动条，默认是会 fade 的
            scrollbarObj: {
                fade: true
            },
            // 这个配置用于做下拉刷新功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启下拉刷新，可以配置顶部下拉的距离（threshold） 来决定刷新时机以及回弹停留的距离（stop）
            pullDownRefreshObj: {
                threshold: 90,
                stop: 40
            },
            // 这个配置用于做上拉加载功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启上拉加载，可以配置离底部距离阈值（threshold）来决定开始加载的时机
            pullUpLoadObj: {
                threshold: 0,
                txt: {
                    more: '加载更多',
                    noMore: '没有更多数据了'
                }
            },
            startY: 0, // 纵轴方向初始化位置
            scrollToX: 0,
            scrollToY: 0,
            scrollToTime: 700,
            pageIndex: 0, // 当前页
            listAllCount: 0, // 总条数
            companyPropShow: false,
            EmergencyDisposalName: '',
            listCount: 0,
            allList: [],
            lists: []
        }
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
        this.onPullingDown()
    },
    methods: {
        openChild (item) {
            this.$set(item, 'open', !item.open)
        },
        onPullingDown () {
            // 模拟下拉刷新
            this.listCount = 0
            this.pageIndex = 0
            this.lists = []
            this.allList = []
            this.getData()
        },
        onPullingUp () {
            this.pageIndex++
            this.getData()
        },
        getData () {
            var params = {
                'allowPaging': true,
                'business': 'GetEmergencyDisposalCard',
                'data': { 'EmergencyDisposalName': this.EmergencyDisposalName }, // 名称
                'pageIndex': this.pageIndex,
                'pageSize': 1000000,
                'userId': this.userInfo.userid
            }
            this.$axiosAjax.getConfigJson(params)
                .then((res) => {
                    if (res.data.code === 0) {
                        let arr = []
                        res.data.data.forEach(item => {
                            if (item.EmergencyDisposalName == '管理人员岗位应急处置卡' && arr[0] && arr[0].EmergencyDisposalName == '管理人员岗位应急处置卡') {
                                arr[0].children.push(item)
                            } else if (item.EmergencyDisposalName == '管理人员岗位应急处置卡') {
                                let itemArr = JSON.parse(JSON.stringify(item))
                                itemArr.children = [item]
                                arr.unshift(itemArr)
                            } else {
                                arr.push(item)
                            }
                        })
                        this.lists = this.lists.concat(arr)
                        this.allList = this.allList.concat(res.data.data)
                        this.listAllCount = res.data.count
                        this.listCount = this.allList.length
                    }
                    if (this.listCount < this.listAllCount) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                }).catch((response) => {
                })
        }
    }
}
</script>
<style scoped>
h3 {
    height: 40px;
    line-height: 40px;
    color: #999;
    font-size: 14px;
    background: #ebeef2;
    padding-right: 20px;
    text-align: right;
}
.betterscrollWrapper {
    position: absolute;
    top: 95px;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}
.betterscroll {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
.recursive-ul .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 20px;
    padding-left: 20px;
    border-bottom: 1px solid #f4f4f4;
}
.recursive-ul .recursive-ul {
    padding-left: 20px;
}
.recursive-ul p {
    line-height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #333;
}
.recursive-ul span {
    color: #999;
}
</style>
